package com.dl7.mvp.music.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.MenuItem;

import com.dl7.mvp.CommonConstant;
import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.base.BaseActivity;
import com.dl7.mvp.base.baseadapter.BaseRecyclerViewAdapter;
import com.dl7.mvp.video.bean.VideoInfo;
import com.dl7.mvp.mvp.R;
import com.dl7.mvp.mvp.databinding.ActivityMusicAlbumsListBinding;
import com.dl7.mvp.video.adapter.VideoAlbumDetailAdapter;
import com.dl7.mvp.video.ui.VideoPlayerActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 专辑包含的视频列表.
 */
public class MusicAlbumsListActivity extends BaseActivity<ActivityMusicAlbumsListBinding> {
    private int mId;
    private String mTitle;
    private ArrayList<VideoInfo> mVideoList;

    public static void launch(Context context, int id, String title) {
        Intent intent = new Intent(context, MusicAlbumsListActivity.class);
        intent.putExtra(CommonConstant.ALBUMS_ID, id);
        intent.putExtra(CommonConstant.ALBUMS_TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mId = getIntent().getIntExtra(CommonConstant.ALBUMS_ID, 0);
        mTitle = getIntent().getStringExtra(CommonConstant.ALBUMS_TITLE);
        setContentView(R.layout.activity_music_albums_list);
        initView();
        loadData();
    }

    private void initView() {
        setTitle(mTitle);
        bindingView.recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //点击back键finish当前activity
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    /**
     * 失败后点击刷新
     */
    protected void onRefresh() {
        loadData();
    }

    private void loadData() {
        RetrofitService.getAlbumDetailList(mId, 0, 999)
                .compose(bindToLife())
                .subscribe(new Observer<List<VideoInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading();
                    }

                    @Override
                    public void onNext(List<VideoInfo> value) {
                        hideLoading();
                        VideoAlbumDetailAdapter adapter = new VideoAlbumDetailAdapter();
                        mVideoList = (ArrayList<VideoInfo>) value;
                        adapter.addDatas(mVideoList);
                        bindingView.recyclerView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        adapter.setOnItemClickListener(new MyOnItemClickListener());
                    }

                    @Override
                    public void onError(Throwable e) {
                        showError();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    class MyOnItemClickListener implements BaseRecyclerViewAdapter.OnItemClickListener<VideoInfo> {
        @Override
        public void onItemClick(int position, VideoInfo data) {
            VideoPlayerActivity.launch(mContext, data);
        }
    }

}
